<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="堆场信息" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-input v-decorator="['yardInfoCol']" rows="4" placeholder="请输入堆场信息" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="收费标准" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="jzxDefaultCostDetailsTable.loading"
          :columns="jzxDefaultCostDetailsTable.columns"
          :dataSource="jzxDefaultCostDetailsTable.dataSource"
          :height="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
        >
          <template v-slot:costName="props">
            <a-select v-model="props.row.costName" allowClear showSearch
                      placeholder="请选择费用名称" style="width: 180px">
              <a-select-option v-for="(item, key) in costNameOptions" :key="key" :value="item.value">
                {{ item.text }}
              </a-select-option>
            </a-select>
          </template>
        </j-vxe-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

import pick from 'lodash.pick'
import { getAction, httpAction } from '@/api/manage'
import { JVxeTableMixin } from '@/mixins/JVxeTableMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { getRefPromise } from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import { ajaxGetDictItems } from '@api/api'

export default {
  name: 'YardInfoForm',
  mixins: [JVxeTableMixin],
  components: {
    JFormContainer,
    ajaxGetDictItems,
    VNodes: {
      functional: true,
      render: (h, ctx) => ctx.props.vnodes
    }
  },
  data() {
    return {
      fastAddModalVisible: false,
      modalConfirmLoading: false,
      addCostName: '',
      addCostCode: '',
      costNameOptions: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {},
      refKeys: ['jzxDefaultCostDetails'],
      tableKeys: ['jzxDefaultCostDetails'],
      activeKey: 'jzxDefaultCostDetails',
      // 集装箱标准费用
      jzxDefaultCostDetailsTable: {
        loading: false,
        dataSource: [],
        columns: [
          // {
          //   title: '费用名称',
          //   key: 'costName',
          //   type: JVXETypes.slot,
          //   width: '200px',
          //   placeholder: '请选择${title}',
          //   slotName: 'action'
          // },
          {
            title: '费用名称',
            key: 'costName',
            type: JVXETypes.slot,
            options: [],
            dictCode: 'cost_name',
            width: '200px',
            placeholder: '请选择${title}',
            defaultValue: '',
            slotName: 'costName'
          },
          {
            title: '币种',
            key: 'currency',
            type: JVXETypes.selectSearch,
            options: [],
            dictCode: 'currency',
            width: '200px',
            placeholder: '请选择${title}',
            defaultValue: ''
          },
          {
            title: '计费单位1',
            key: 'unit1',
            type: JVXETypes.selectSearch,
            options: [],
            dictCode: 'box_rate',
            width: '200px',
            placeholder: '请选择${title}',
            defaultValue: ''
          },
          {
            title: '计费单位2',
            key: 'unit2',
            type: JVXETypes.selectSearch,
            options: [],
            dictCode: 'box_rate',
            width: '200px',
            placeholder: '请选择${title}',
            defaultValue: ''
          },
          {
            title: '单价',
            key: 'price',
            type: JVXETypes.inputNumber,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: ''
          }
        ]
      },
      url: {
        add: '/box/yardInfo/add',
        edit: '/box/yardInfo/edit',
        queryById: '/box/yardInfo/queryById',
        jzxDefaultCostDetails: {
          list: '/box/yardInfo/queryJzxDefaultCostDetailsByMainId'
        }
      }
    }
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：false流程表单 true普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.initDictData()
  },
  methods: {
    addBefore() {
      this.form.resetFields()
      this.jzxDefaultCostDetailsTable.dataSource = []
    },
    getAllTable() {
      let values = this.tableKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      let fieldval = pick(this.model, 'yardInfoCol')
      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.jzxDefaultCostDetails.list, params, this.jzxDefaultCostDetailsTable)
      }
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        jzxDefaultCostDetailsList: allValues.tablesValue[0].tableData
      }
    },
    //渲染流程表单数据
    // showFlowData() {
    //   if (this.formBpm === true) {
    //     let params = { id: this.formData.dataId }
    //     getAction(this.url.queryById, params).then((res) => {
    //       if (res.success) {
    //         this.edit(res.result)
    //       }
    //     })
    //   }
    // },
    validateError(msg) {
      this.$message.error(msg)
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'yardInfoCol'))
    },
    initDictData() {
      let param = { isLock: 0, pageSize: 99999 }
      getAction('/fee/list', param).then(res => {
        console.log(res.result)
        let feeData = []
        for (let i = 0; i < res.result.length; i++) {
          feeData.push({
            value: res.result[i].feeName,
            text: res.result[i].feeName
          })
        }
        this.costNameOptions = feeData
      })
    },
    modalAddHandleCancel(e) {
      this.modalConfirmLoading = false
      this.fastAddModalVisible = false
      this.addCostCode = ''
      this.addCostName = ''
    },
    modalAddHandleOk(e) {
      // this.customerModalVisible = false
      let costName = this.addCostName.trim().replaceAll(' ', '')
      let costCode = this.addCostCode.trim().replaceAll(' ', '')
      let url = '/sys/dictItem/add', method = 'post'
      let value = { 'itemValue': costCode, 'itemText': costName, 'dictId': '1319479895549579266', 'status': 1 }
      if (costName && costCode) {
        httpAction(url, value, method).then((res) => {
          if (res.success) {
            console.log(res.success)
            this.$message.success(res.message)
            this.fastAddModalVisible = false
            this.costNameOptions.unshift({ 'value': costCode, 'text': costName, 'title': costName })
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.addCostCode = ''
          this.addCostName = ''
        })
      } else {
        this.$message.warning('请输入费用信息')
      }
    }

  }
}
</script>

<style scoped>
</style>